package com.ecr.hub.front.bean.listener;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ecr.hub.front.bean.managed.LoginManagedBean;

public class SessionFilter implements Filter {

	@SuppressWarnings("unused")
	private FilterConfig config;

	@Override
	public void destroy() {
		config = null;
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain fc) throws IOException, ServletException {
		LoginManagedBean loginBean = (LoginManagedBean) (((HttpServletRequest) req)
				.getSession().getAttribute(LoginManagedBean.LOGIN_BEAN));
		if (loginBean != null && loginBean.getCurrentUser() != null) {
			fc.doFilter(req, resp);
		} else {
			((HttpServletResponse) resp)
					.sendRedirect("/hub/faces/login/login.jsf");
		}
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		this.config = arg0;
	}
}
